﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
using ServiceStack.Text;
using ServiceStack.Redis.Generic;
namespace ShiNiu.ServiceStackRedis
{
    public class SnRedisBase : IDisposable
    {

        // <summary>
        /// redis配置文件信息
        /// </summary>
        private static RedisConfig Config = RedisConfig.GetConfig();
        //public static IRedisClient Core { get; private set; }
        //public   RedisClient Redis = new RedisClient(); 
        private static readonly object obj = new object();
        //缓存池
        //public static PooledRedisClientManager prcm = new PooledRedisClientManager();
        public static PooledRedisClientManager prcm = null;
        private static readonly Lazy<IRedisClient> RedisLazy = new Lazy<IRedisClient>(() =>
        {
            IRedisClient redisClient = prcm.GetClient();
            redisClient.Password = Config.PassWord;
            return redisClient;
        });

        static SnRedisBase()
        { 
            if (prcm == null)
            {
                lock (obj)
                {
                    if (prcm == null)
                    {
                        prcm = CreateManager();
                    }
                }
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="OpenPooledRedis">是否开启缓冲池</param>
        public SnRedisBase()
        {
        }

        /// <summary>
        /// 缓冲池
        /// </summary>
        /// <param name="readWriteHosts"></param>
        /// <param name="readOnlyHosts"></param>
        /// <returns></returns>
        public static PooledRedisClientManager CreateManager()
        { 
            string[] WriteServerConStr = SplitString(Config.WriteServerConStr, ",");
            string[] ReadServerConStr = SplitString(Config.ReadServerConStr, ",");
            RedisClientManagerConfig RedisClientConfig = new RedisClientManagerConfig
            {
                MaxWritePoolSize = Config.MaxWritePoolSize,
                MaxReadPoolSize = Config.MaxReadPoolSize,
                AutoStart = Config.AutoStart,
            };
            return prcm = new PooledRedisClientManager(ReadServerConStr, WriteServerConStr, RedisClientConfig) { ConnectTimeout = 1000 * 60 * 20 }; 
        }

        //弃用
        //[Obsolete("ClientRedis弃用 使用RedisClient()")]
        public static IRedisClient ClientRedis
        {
            get
            {
                return RedisLazy.Value;
            }
        }

        public static IRedisClient RedisClient()
        {
            IRedisClient redisClient = prcm.GetClient();
            redisClient.Password = Config.PassWord; 
            return redisClient;
        }

        private static string[] SplitString(string strSource, string split)
        {
            return strSource.Split(split.ToArray());
        }

        #region IDisposable成员
        public void Dispose()
        {
            this.ExplicitDispose();
        }
        #endregion

        #region 
        protected void ExplicitDispose()
        {
            this.Dispose(true);
            GC.Collect();
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            if (disposing)//清除非托管资源
            {
                //RedisClient = null;
                ClientRedis.Dispose();
            }
        }
        #endregion

        ~SnRedisBase()
        {
            this.Dispose(false);
        }

    }
}
